package reports;

import static org.junit.Assert.*;

import java.io.File;
import java.text.ParseException;
import java.util.Date;

import org.junit.Test;

import reports.visitor.IVisitor;

import common.util.DateUtils;

/**
 * Tests to be sure the generated files from the Product Statistics Report are valid
 * 
 * @author Ryan Cheatham
 */
public class ProductStatisticsReportGenerationTest extends ProductStatisticsReportTestSuperclass
{
  @Test
  public void toHTMLTest()
  {
    setItemManagerVisitorNode(new MockNode()
    {
      @Override
      public void preOrderAccept(IVisitor visitor)
      {
        try
        {
          for (int i = 1; i <= 31; i++)
          {
            Date date = DateUtils.parseDate(String.format("07/%02d/2013", i));
            generateItem(date).preOrderAccept(visitor);
          }
        }
        catch (ParseException e)
        {
          fail(e.getMessage());
        }
      }
    });

    setProductManagerVisitorNode(new MockNode()
    {
      @Override
      public void preOrderAccept(IVisitor visitor)
      {
        getDefaultProduct().preOrderAccept(visitor);
      }
    });

    Report report = generateReport(5);
    File file = report.toHtml();

    assertNotNull(file);
    assertTrue(file.exists());
    assertTrue(file.isFile());
    assertTrue(file.canRead());
    assertTrue(file.getName().endsWith(".html"));
    assertNotSame(0, file.length());
  }

  @Test
  public void toPDFTest()
  {
    setItemManagerVisitorNode(new MockNode()
    {
      @Override
      public void preOrderAccept(IVisitor visitor)
      {
        try
        {
          for (int i = 1; i <= 31; i++)
          {
            Date date = DateUtils.parseDate(String.format("07/%02d/2013", i));
            generateItem(date).preOrderAccept(visitor);
          }
        }
        catch (ParseException e)
        {
          fail(e.getMessage());
        }
      }
    });

    setProductManagerVisitorNode(new MockNode()
    {
      @Override
      public void preOrderAccept(IVisitor visitor)
      {
        getDefaultProduct().preOrderAccept(visitor);
      }
    });

    Report report = generateReport(5);
    File file = report.toPdf();

    assertNotNull(file);
    assertTrue(file.exists());
    assertTrue(file.isFile());
    assertTrue(file.canRead());
    assertTrue(file.getName().endsWith(".pdf"));
    assertNotSame(0, file.length());
  }
}
